package com.valveinspectionsys.routing.activity;
import com.valveinspectionsys.R;
import com.valveinspectionsys.povo.SpinnerVo;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class DeviceRoutingQueryActivityGroup  extends ActivityGroup {
    
    private LinearLayout container = null;

    private RadioGroup radiogroup = null;

    private RadioButton radio2 = null;// 水表信息

    private RadioButton radio3 = null;// 数据统计

    private RadioButton radio4 = null;// 选择条件

    private RadioButton radio1 = null;// 水表列表信息
    
    private DeviceRoutingConditionActivity conditionAct;
    
    private DeviceListInfoActivity  listAct;
    
    private DeviceInfoActivity   infoAct;
    
    private String conditionName;
    
    private String conditionNumber;
    
    private String conditionStatus;
    
    private  String conditionAdds;
    
    private String deviceid;//设备ID
    
    private String deviceType;//设备类型
    
    private String deviceListid;//设备册
    
    
    private String infolistid;//详细设备册
    
    
    
    
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.querywaterlist);
	getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
	inti();
	
    }

    /**
     * 初始化控件并添加事件
     * 
     */
    public void inti() {
	container = (LinearLayout) findViewById(R.id.container1);
	radiogroup = (RadioGroup) findViewById(R.id.querywater_main_radio);
	radio1 = (RadioButton) findViewById(R.id.waterlist_info);//设备信息
	radio2 = (RadioButton) findViewById(R.id.waterlist_waterinfo);//设备列表
       //radio3 = (RadioButton) findViewById(R.id.waterlist_querywaterlist);
	radio4 = (RadioButton) findViewById(R.id.waterlist_querycondition);//查询条件

	radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

	    @Override
	    public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (checkedId == radio2.getId()) {
		    launchActivity("DeviceInfoActivity", DeviceInfoActivity.class, null);
		    infoAct=(DeviceInfoActivity)getCurrentActivity();
		    infoAct.initDate();
		} else if (checkedId == radio1.getId()) {


	                   launchActivity("DeviceListInfoActivity", DeviceListInfoActivity.class, null);
			   listAct=(DeviceListInfoActivity)getCurrentActivity();
			   listAct.queryDater();
			   ListView listview = listAct.getListView();
			   listview.setOnItemClickListener(new OnItemClickListener() {

			    @Override
			    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				  deviceid=listAct.list.get(position).getDeviceId();
				  infolistid=listAct.list.get(position).getDeviceListId();
				  System.out.println("-----infolistid------"+infolistid);
				  radio2.setChecked(true);
				
			    }
			});

		} else if (checkedId == radio4.getId()) {
		   
		    launchActivity("DeviceRoutingConditionActivity", DeviceRoutingConditionActivity.class, null);
		    conditionAct=(DeviceRoutingConditionActivity)getCurrentActivity();
		    conditionAct.button.setOnClickListener(new OnClickListener() {
		        
		        @Override
		        public void onClick(View v) {
		    	        conditionStatus=conditionAct.getDevicestatus();
		    	        conditionNumber=conditionAct.edit1.getEditableText().toString();
		    	        deviceType=conditionAct.getDeviceType();
		    	        conditionAdds=conditionAct.edit3.getEditableText().toString();
		    	        SpinnerVo spinner=(SpinnerVo)conditionAct.spinner1.getSelectedItem();
		    	        deviceListid=spinner.getKey();
		    	        System.out.println("-------------key--------"+deviceListid);
		    	         radio1.setChecked(true);
		        }
		    });
		}

	    }
	});
	radio4.setChecked(true);
    }

    /**
     * 打开子Activity的菜单事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_MENU) {
	    getCurrentActivity().openOptionsMenu();

	}
	return super.onKeyDown(keyCode, event);
    }

    /**
     * 界面切换
     * 
     * @param id
     * @param activityClass
     */
    public void launchActivity(String id, Class<?> activityClass, Bundle bundle) {
	container.removeAllViews();

	Intent intent = new Intent(DeviceRoutingQueryActivityGroup.this, activityClass);

	if (bundle != null) {
	    intent.putExtras(bundle);
	}
	intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

	Window window = getLocalActivityManager().startActivity(id, intent);
	View view = window.getDecorView();
	container.addView(view);
    }

    @Override
    protected void onDestroy() {
	
	super.onDestroy();
    }

    public String getConditionName() {
        return conditionName;
    }

    public void setConditionName(String conditionName) {
        this.conditionName = conditionName;
    }

    public String getConditionNumber() {
        return conditionNumber;
    }

    public void setConditionNumber(String conditionNumber) {
        this.conditionNumber = conditionNumber;
    }

    public String getConditionStatus() {
        return conditionStatus;
    }

    public void setConditionStatus(String conditionStatus) {
        this.conditionStatus = conditionStatus;
    }

    public String getConditionAdds() {
        return conditionAdds;
    }

    public void setConditionAdds(String conditionAdds) {
        this.conditionAdds = conditionAdds;
    }

    public String getDeviceid() {
        return deviceid;
    }

    public void setDeviceid(String deviceid) {
        this.deviceid = deviceid;
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	// 获取当前活动的Activity实例
	Activity subActivity = getLocalActivityManager().getCurrentActivity();
	// 判断是否实现返回值接口
	
	if(subActivity instanceof DeviceInfoActivity){
	    DeviceInfoActivity listener=(DeviceInfoActivity)subActivity;
	    listener.handleActivityResult(requestCode, resultCode, data);
	}
	super.onActivityResult(requestCode, resultCode, data);

    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getDeviceListid() {
        return deviceListid;
    }

    public void setDeviceListid(String deviceListid) {
        this.deviceListid = deviceListid;
    }

    public String getInfolistid() {
        return infolistid;
    }

    public void setInfolistid(String infolistid) {
        this.infolistid = infolistid;
    }   
}
